/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package client.ui.fileTransmiting;

import client.ui.fileTransmiting.removingTransmitedFile.RemoveProducer;
import java.io.*;
import java.net.Socket;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
import socketHandler.MessageSocketHandler;

/**
 *
 * @author lex
 */
public class SendingFille extends TransmitedFile{

    public SendingFille(String userName, File f, RemoveProducer canProd, long size) {
	super(userName, f, canProd, size);
    }

    @Override
    public void Start(Socket socket) 
    {
        soc=socket;
	started=true;
        new Thread(new Runnable()
        {
            public void run()
            {
                jButton1.setEnabled(true);
                FileInputStream fis=null;
                try {
                    fis=new FileInputStream(f);
                } catch (FileNotFoundException ex) {}
                try {
		    long readed_res=0;
                    byte[] buf=new byte[buf_len];
                    while (readed_res<size)
                    {
                        int readed=fis.read(buf);
			readed_res+=readed;
			if (readed<1) break;
                        soc.getOutputStream().write(buf, 0, readed);
                        synchronized(stop)
                        {
                            if (stop) break;
                        }
			setProgress((int)(readed_res*100/size));
                    }
		    remProd.remove(nickName, f.getAbsolutePath());
		    soc.close();
		    fis.close();
                } catch (IOException ex) {
                    remProd.remove(nickName, f.getAbsolutePath());
                    try {
                        soc.close();
                    } catch (IOException ex1) {}
                    try {
                        fis.close();
                    } catch (IOException ex1) {}
                }
            }
        }).start();
    }
}
